<template>
  <el-dialog
    :title="!dataForm.receiptId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="回款日期" prop="receiptDate">
      <el-input v-model="dataForm.receiptDate" placeholder="回款日期"></el-input>
    </el-form-item>
    <el-form-item label="业务员" prop="salesman">
      <el-input v-model="dataForm.salesman" placeholder="业务员"></el-input>
    </el-form-item>
    <el-form-item label="汇款人" prop="remitter">
      <el-input v-model="dataForm.remitter" placeholder="汇款人"></el-input>
    </el-form-item>
    <el-form-item label="客户名称" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="当月回款" prop="monthlyReceipt">
      <el-input v-model="dataForm.monthlyReceipt" placeholder="当月回款"></el-input>
    </el-form-item>
    <el-form-item label="本年累计回款" prop="yearToDateReceipt">
      <el-input v-model="dataForm.yearToDateReceipt" placeholder="本年累计回款"></el-input>
    </el-form-item>
    <el-form-item label="回款银行" prop="receiptBank">
      <el-input v-model="dataForm.receiptBank" placeholder="回款银行"></el-input>
    </el-form-item>
    <el-form-item label="待冲金额" prop="toBeOffset">
      <el-input v-model="dataForm.toBeOffset" placeholder="待冲金额"></el-input>
    </el-form-item>
    <el-form-item label="上月待冲" prop="lastMonthToBeOffset">
      <el-input v-model="dataForm.lastMonthToBeOffset" placeholder="上月待冲"></el-input>
    </el-form-item>
    <el-form-item label="已冲金额" prop="alreadyOffset">
      <el-input v-model="dataForm.alreadyOffset" placeholder="已冲金额"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="录入人" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="录入人"></el-input>
    </el-form-item>
    <el-form-item label="录入时间" prop="createdTime">
      <el-input v-model="dataForm.createdTime" placeholder="录入时间"></el-input>
    </el-form-item>
    <el-form-item label="有效性验证" prop="isValid">
      <el-input v-model="dataForm.isValid" placeholder="有效性验证"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          receiptId: 0,
          receiptDate: '',
          salesman: '',
          remitter: '',
          customerName: '',
          monthlyReceipt: '',
          yearToDateReceipt: '',
          receiptBank: '',
          toBeOffset: '',
          lastMonthToBeOffset: '',
          alreadyOffset: '',
          remarks: '',
          createdBy: '',
          createdTime: '',
          isValid: ''
        },
        dataRule: {
          receiptDate: [
            { required: true, message: '回款日期不能为空', trigger: 'blur' }
          ],
          salesman: [
            { required: true, message: '业务员不能为空', trigger: 'blur' }
          ],
          remitter: [
            { required: true, message: '汇款人不能为空', trigger: 'blur' }
          ],
          customerName: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          monthlyReceipt: [
            { required: true, message: '当月回款不能为空', trigger: 'blur' }
          ],
          yearToDateReceipt: [
            { required: true, message: '本年累计回款不能为空', trigger: 'blur' }
          ],
          receiptBank: [
            { required: true, message: '回款银行不能为空', trigger: 'blur' }
          ],
          toBeOffset: [
            { required: true, message: '待冲金额不能为空', trigger: 'blur' }
          ],
          lastMonthToBeOffset: [
            { required: true, message: '上月待冲不能为空', trigger: 'blur' }
          ],
          alreadyOffset: [
            { required: true, message: '已冲金额不能为空', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '录入人不能为空', trigger: 'blur' }
          ],
          createdTime: [
            { required: true, message: '录入时间不能为空', trigger: 'blur' }
          ],
          isValid: [
            { required: true, message: '有效性验证不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.receiptId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.receiptId) {
            this.$http({
              url: this.$http.adornUrl(`/cw/receipts/info/${this.dataForm.receiptId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.receiptDate = data.receipts.receiptDate
                this.dataForm.salesman = data.receipts.salesman
                this.dataForm.remitter = data.receipts.remitter
                this.dataForm.customerName = data.receipts.customerName
                this.dataForm.monthlyReceipt = data.receipts.monthlyReceipt
                this.dataForm.yearToDateReceipt = data.receipts.yearToDateReceipt
                this.dataForm.receiptBank = data.receipts.receiptBank
                this.dataForm.toBeOffset = data.receipts.toBeOffset
                this.dataForm.lastMonthToBeOffset = data.receipts.lastMonthToBeOffset
                this.dataForm.alreadyOffset = data.receipts.alreadyOffset
                this.dataForm.remarks = data.receipts.remarks
                this.dataForm.createdBy = data.receipts.createdBy
                this.dataForm.createdTime = data.receipts.createdTime
                this.dataForm.isValid = data.receipts.isValid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cw/receipts/${!this.dataForm.receiptId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'receiptId': this.dataForm.receiptId || undefined,
                'receiptDate': this.dataForm.receiptDate,
                'salesman': this.dataForm.salesman,
                'remitter': this.dataForm.remitter,
                'customerName': this.dataForm.customerName,
                'monthlyReceipt': this.dataForm.monthlyReceipt,
                'yearToDateReceipt': this.dataForm.yearToDateReceipt,
                'receiptBank': this.dataForm.receiptBank,
                'toBeOffset': this.dataForm.toBeOffset,
                'lastMonthToBeOffset': this.dataForm.lastMonthToBeOffset,
                'alreadyOffset': this.dataForm.alreadyOffset,
                'remarks': this.dataForm.remarks,
                'createdBy': this.dataForm.createdBy,
                'createdTime': this.dataForm.createdTime,
                'isValid': this.dataForm.isValid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
